//
//  SimpleSeg.m
//  YueMall
//
//  Created by a on 15/7/16.
//  Copyright (c) 2015年 yindan. All rights reserved.
//

#import "SimpleSeg.h"
@interface SimpleSeg(){
    
}
@property(nonatomic,assign)CGFloat elementWidth;
@property(nonatomic,assign)CGFloat elementHeight;
@property(nonatomic,strong)UIView *bottomView;
@end;
@implementation SimpleSeg
@synthesize bgview = _bgview;
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
-(id)initWithFrame:(CGRect)frame delegate:(id)delegate{
    return [self initWithFrame:frame delegate:delegate isNotFitForFullWidth:NO];
}
-(id)initWithFrame:(CGRect)frame delegate:(id)delegate isNotFitForFullWidth:(BOOL)isNotFitForFullWidth{
    if (self==[super initWithFrame:frame]) {
        self.segdelegate=delegate;
        self.isNotFitForFullDeviceWidth = isNotFitForFullWidth;
        
        _bgview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _bgview.userInteractionEnabled=YES;
        _bgview.backgroundColor=[UIColor clearColor];
        _bgview.scrollEnabled = NO;
        [self addSubview:_bgview];
        
        [self createElementsForSeg];
    }
    return self;
}
-(void)reload{
    [self.bottomView removeFromSuperview];
    for (UIView *view in _bgview.subviews) {
        [view removeFromSuperview];
    }
    self.isDefineAnimate = YES;
    [self createElementsForSeg];
}
-(void)reloadBadgeNum{
    if ([self.segdelegate respondsToSelector:@selector(badgeNumForSeg)]) {
        NSArray *badgeNumArray = [self.segdelegate badgeNumForSeg];
//        [badgeNumArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            UIButton *button = [self viewWithTag:7000+idx];
//            [button showBadgeWithStyle:WBadgeStyleNumber value:[obj intValue] animationType:WBadgeAnimTypeNone];
//            button.badgeCenterOffset = CGPointMake(-15, 12);
//            button.badgeBgColor = [UIColor redColor];
//            button.badgeTextColor = [UIColor whiteColor];
//        }];
        
    }
}
-(void)createElementsForSeg{
    NSArray *arraySeg=[self.segdelegate titlesForSeg];
    self.elementWidth = 75;
    _bgview.contentSize = CGSizeMake(75*[arraySeg count], 50);
    if (!self.isNotFitForFullDeviceWidth) {
        self.elementWidth = _bgview.width/[arraySeg count];
        _bgview.contentSize = CGSizeMake(_bgview.width, 50);
    }
    
    self.elementHeight=self.frame.size.height;
    [arraySeg enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(idx*self.elementWidth, 0, self.elementWidth, self.elementHeight)];
        [button setTitle:[arraySeg objectAtIndex:idx] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        if (idx==0) {
            button.selected=YES;
        }
        button.tag=7000+idx;
        [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [button.titleLabel setAdjustsFontSizeToFitWidth:YES];
        [button addTarget:self action:@selector(segBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgview addSubview:button];
        
        if ([self.segdelegate respondsToSelector:@selector(badgeNumForSeg)]) {
            NSArray *badgeNumArray = [self.segdelegate badgeNumForSeg];
//            if ([badgeNumArray count]>idx) {
//                [button showBadgeWithStyle:WBadgeStyleNumber value:[[badgeNumArray objectAtIndex:idx] intValue] animationType:WBadgeAnimTypeNone];
//                button.badgeCenterOffset = CGPointMake(-15, 12);
//                button.badgeBgColor = [UIColor redColor];
//                button.badgeTextColor = [UIColor whiteColor];
//            }
        }
    }];
    
    self.bottomView  = [[UIView alloc] initWithFrame:CGRectMake(self.elementWidth/2-64/2,self.frame.size.height-3,64, 2)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [_bgview addSubview:self.bottomView];
}
-(void)setSelectIndex:(int)index{
    UIButton *button = [self viewWithTag:7000+index];
    [self segBtnPressed:button];
}
-(void)setSelect:(NSInteger)index{
    UIButton *button = [self viewWithTag:7000+index];
    for (UIView *view in _bgview.subviews) {
        if (view.tag>=7000) {
            UIButton *button1=(UIButton*)view;
            button1.selected=NO;
        }
    }
    button.selected=YES;
    {
        [UIView animateWithDuration:0.3 animations:^
         {
             self.bottomView.frame = CGRectMake(self.elementWidth*(button.tag-7000)+self.elementWidth/2-64/2,self.frame.size.height-3,64, 2);
         }];
    }
}
-(void)segBtnPressed:(id)sender{
    UIButton *button=(UIButton*)sender;
    for (UIView *view in _bgview.subviews) {
        if (view.tag>=7000) {
            UIButton *button1=(UIButton*)view;
            button1.selected=NO;
        }
    }
    button.selected=YES;
    {
        if (self.isDefineAnimate) {
             self.bottomView.frame = CGRectMake(self.elementWidth*(button.tag-7000)+self.elementWidth/2-64/2,self.frame.size.height-3,64, 2);
        }else{
            [UIView animateWithDuration:0.3 animations:^
             {
                 self.bottomView.frame = CGRectMake(self.elementWidth*(button.tag-7000)+self.elementWidth/2-64/2,self.frame.size.height-3,64, 2);
             }];
        }
        self.isDefineAnimate = NO;
    }
    if (self.segdelegate&&[self.segdelegate respondsToSelector:@selector(simpleSeg:didselectIndex:)]) {
        [self.segdelegate simpleSeg:self didselectIndex:button.tag-7000];
    }
}
-(void)updateTitles:(NSArray*)array{
    for (UIView *view in _bgview.subviews) {
        if (view.tag>=7000) {
            UIButton *button=(UIButton*)view;
            NSLog(@"__________________%@",button.titleLabel.text);
            [button setTitle:[array objectAtIndex:button.tag-7000] forState:UIControlStateNormal];
        }
    }
}
@end
